<?php
class h5 extends base{

	public function init(){
		parent::init();

		$this->tit = '统计';

		$this->companyObj = D('company');
        $this->orderObj = D('company_order');

        $this->cateObj = D('company_cate');
        $this->adminObj = D('company_admin');
	}


    public function index()
	{


        // 今日
        $s_time = date('Y-m-d 00:00:00');
        // $e_time = date('Y-m-d 00:00:00');
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>'{$s_time}'";
		$count1 = $this->orderObj->findCount(array($cond));

        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
		$count2 = $this->orderObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
		$count3 = $this->orderObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
		$count4 = $this->orderObj->findCount(array($cond));



        // 数据概况
        $this->survey = [
            'count1' => $count2,
            'count2' => $count3,
            'count3' => $count4,
        ];

        json_encode($survey,true);

        // 数据走势图


        // 最近七天日期
        $lately7days = $this->getLately7days();

		$data = [
            'widget-card' => [
                'count1' => $count1,
                'count2' => $count2,
                'count3' => $count3,
                'count4' => $count4,
            ],
            // 'widget-outline' => [
            //     'order_total_price' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     'order_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     // 新增用户数
            //     'new_user_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ],
            //     // 下单用户数
            //     'order_user_total' => [
            //         'tday' => 0,
            //         'ytd' => 0
            //     ]
            // ],
            'widget-echarts' => [
                // 最近七天日期
                'date' => json_encode($lately7days,true),
                'order_total' => json_encode($this->getOrderTotalByDate($lately7days),true),
                'order_total_price' => json_encode($this->getOrderTotalByDate2($lately7days),true),
            ]
		];
		$this->data = $data;
        // dump($this->data);exit;


        // 数据排行
        // 分类排行
        $cates = $this->cateObj->findAll(array('status=1'.$this->cond_base));
        if($cates){
            foreach($cates as $val){
                $v[$val['id']] = $this->getOrderTotalByCate($val['id']);
                $v[$val['id']]['name'] = $val['name'];
            }
        }
        $cateRanking = $v;
        $count2 = array_column($cateRanking, 'count2');
        $count3 = array_column($cateRanking, 'count3');
        array_multisort($count2, SORT_DESC, $count3, SORT_DESC, $cateRanking);


         // 师傅排行
         $admins = $this->adminObj->findAll(array('status=1 and (level=3 or level=4)'.$this->cond_base));
         if($admins){
             foreach($admins as $val){
                 $v2[$val['id']] = $this->getOrderTotalByAdmin($val['id']);
                 $v2[$val['id']]['name'] = $val['realname'];
             }
         }
         $adminRanking = $v2;
         $count2 = array_column($adminRanking, 'count2');
         $count3 = array_column($adminRanking, 'count3');
         array_multisort($count2, SORT_DESC, $count3, SORT_DESC, $adminRanking);

        // dump($cateRanking);exit;

        $this->cateRanking = $cateRanking;
        $this->adminRanking = $adminRanking;



	}


    /**
     * 数据概况API
     * @param null $startDate
     * @param null $endDate
     * @return array
     * @throws \think\Exception
     */
    public function survey()
    {
		$startDate = arg('startDate');
        $endDate = arg('endDate');
        // 今日
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>'{$startDate}' and add_time>'{$endDate}'";
		$count1 = $this->orderObj->findCount(array($cond));

        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>'{$startDate}' and add_time>'{$endDate}'";
		$count2 = $this->orderObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>'{$startDate}' and add_time>'{$endDate}'";
		$count3 = $this->orderObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
        $cond .= " and add_time>'{$startDate}' and add_time>'{$endDate}'";
		$count4 = $this->orderObj->findCount(array($cond));


        $survey = [
            'count1' => $count2,
            'count2' => $count3,
            'count3' => $count4,
        ];
        
        $this->renderSuccess('', '',$survey);
		
    }


    
    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    private function getOrderTotalByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal($day);
        }
        return $data;
    }

    private function getOrderTotal($day = null)
    {
        // 今日
        $s_time = date("{$day} 00:00:00");
        $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->orderObj->findCount(array($cond));
        
        return number_format($count1);
    }

    private function getOrderTotalByDate2($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = $this->getOrderTotal2($day);
        }
        return $data;
    }

    private function getOrderTotal2($day = null)
    {
        // 今日
        $s_time = date("{$day} 00:00:00");
        $e_time = date("{$day} 23:59:59");
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";
		$count1 = $this->orderObj->findCount(array($cond));
        
        return number_format($count1);
    }


    private function getOrderTotalByCate($cate_id)
    {
        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and cate_id='{$cate_id}'";
		$count2 = $this->orderObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and cate_id='{$cate_id}'";
		$count3 = $this->orderObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
        $cond .= " and cate_id='{$cate_id}'";
		$count4 = $this->orderObj->findCount(array($cond));

        $data['count2'] = number_format($count2);
        $data['count3'] = number_format($count3);
        $data['count4'] = number_format($count4);

        
        return $data;
    }


    private function getOrderTotalByAdmin($admin_id)
    {
        // 总量
		$cond = "status=1 and order_status<99 ".$this->cond_base;
        $cond .= " and shifu_id='{$admin_id}'";
		$count2 = $this->orderObj->findCount(array($cond));

        // 已完成
        $cond = "status=1 and order_status>=5 and order_status<99 ".$this->cond_base;
        $cond .= " and shifu_id='{$admin_id}'";
		$count3 = $this->orderObj->findCount(array($cond));

        // 未完成
        $cond = "status=1 and order_status<5 and order_status>1 ".$this->cond_base;
        $cond .= " and shifu_id='{$admin_id}'";
		$count4 = $this->orderObj->findCount(array($cond));

        $data['count2'] = number_format($count2);
        $data['count3'] = number_format($count3);
        $data['count4'] = number_format($count4);

        
        return $data;
    }

    

    
	/**
     * 数据统计主页
     * @return mixed
     * @throws \think\Exception
     */
    public function index2()
    {
        // $this->survey = $this->siteObj->getSurveyData();
        // $this->echarts7days = $this->siteObj->getTransactionTrend();
        // $this->goodsRanking = $this->siteObj->getGoodsRanking();
        // $this->userExpendRanking = $this->siteObj->geUserExpendRanking();

        $this->survey = [
                // 用户数量
                'user_total' => 0,
                // 消费人数
                'consume_users' => 0,
                // 付款订单数
                'order_total' => 0,
                // 付款订单总额
                'order_total_money' => 0,
                // 商品总量
                'goods_total' => 0,
                // 用户充值总额
                'recharge_total' => 0,
            ];
        $this->echarts7days = [
                'date' => [],
                'order_total' => [],
                'order_total_price' => []
            ];
        $this->goodsRanking = [];
        $this->userExpendRanking = [];
    }

    

}